package com.example.everying.controller.content;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.everying.annotation.WebLog;
import com.example.everying.context.UserContext;
import com.example.everying.model.entity.content.ActivityApply;
import com.example.everying.model.entity.content.ActivityArticle;
import com.example.everying.model.request.AppViewRequest;
import com.example.everying.model.vo.content.ActivityApplyVo;
import com.example.everying.model.vo.content.ActivityArticleVo;
import com.example.everying.model.vo.user.UserInfoVo;
import com.example.everying.service.content.ActivityApplyService;
import com.example.everying.utils.PageParam;
import com.example.everying.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: everything
 * @description: 申请人控制层
 * @author: zw
 * @create: 2025-05-16 10:10
 **/
@Slf4j
@RestController
@Api(tags = "活动申请人")
@RequestMapping("activity/apply")
public class ActivityApplyController {

    @Autowired
    private ActivityApplyService applyService;


    //条件查询带分页
    @ApiOperation(value = "分页查询")
    @PostMapping("/page")
    public Result list(@RequestBody PageParam<ActivityApplyVo> query) {
        Page<ActivityApply> pageParam = new Page<>(query.getCurrent(), query.getLimit());
        IPage<ActivityApply> pageModel = applyService.selectPage(pageParam, query.getQuery());
        return Result.ok(pageModel);
    }

    //条件查询带分页
    @ApiOperation(value = "列表查询")
    @GetMapping("/list")
    @WebLog(description = "申请人列表")
    public Result<List<ActivityApplyVo>> list(String artId) {
        return Result.success(applyService.applyList(artId));
    }

    @PostMapping("/appRead")
    @WebLog(description = "申请人列表待查看状态")
    public Result appRead(@RequestBody AppViewRequest request) {
        return Result.success(applyService.appRead(request.getArtId(),request.getOrdersStatus()));
    }
    @ApiOperation(value = "添加内容")
    @PostMapping("applyAdd")
    @WebLog(description = "添加申请人")
    public Result<?> applyAdd(@ApiParam(name = "ActivityApplyVo", value = "申请人", required = true)
                           @RequestBody ActivityApplyVo applyVo) {
      return applyService.applyAdd(applyVo);
    }

    @ApiOperation(value = "修改")
    @ApiParam(name = "id", value = "id", required = true)
    @PostMapping("applyEdit")
    @WebLog(description = "申请人修改")
    public Result<?> applyEdit(@ApiParam(name = "ActivityApplyVo", value = "申请人", required = true)
                            @RequestBody ActivityApplyVo applyVo) {
        UserInfoVo user = UserContext.getUser();
        if (user == null){
            return Result.fail("请先登录");
        }
        return applyService.applyEdit(applyVo) ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "通过id逻辑删除")
    @ApiParam(name = "id", value = "id", required = true)
    @PostMapping("delete")
    public Result delete(@ApiParam(name = "ActivityApplyVo", value = "申请人", required = true)
                         @RequestBody ActivityArticleVo contentVo) {
        ActivityArticle article = new ActivityArticle();
        BeanUtil.copyProperties(contentVo, article);
        applyService.removeById(article);
        return Result.ok();
    }


}
